﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
  public  class BillDetailManager
    {
        private DataTable CreateTable(List<BillDetail> list)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("BillID", Type.GetType("System.Int32"));
            tb.Columns.Add("ProductID", Type.GetType("System.String"));
            tb.Columns.Add("Quantity", Type.GetType("System.Int32"));
            tb.Columns.Add("UnitPrice", Type.GetType("System.Int64"));
            tb.Columns.Add("Status", Type.GetType("System.String"));
            foreach (BillDetail a in list)
            {
                DataRow newrow = tb.NewRow();
                object[] array = new object[5];
                array[0] = a.BillID;
                array[1] = a.ProductID;
                array[2] = a.Quantity;
                array[3] = a.UnitPrice;
                array[4] = a.Status;
                newrow.ItemArray = array;
                tb.Rows.Add(newrow);
            }
            return tb;
        }
        public DataTable GetAll()
        {
            List<BillDetail> BillDetailList = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                BillDetailList = (from a in ctx.BillDetails where a.Status.CompareTo("Đang tồn tại") == 0 select a).ToList<BillDetail>();
            }
            return CreateTable(BillDetailList);
        }
        public DataTable SearchID(int ID)
        {
            List<BillDetail> billdetails = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                billdetails = ctx.BillDetails.Where(a => a.BillID == ID&&a.Status.CompareTo("Đang tồn tại")==0).ToList<BillDetail>();
            }
            return CreateTable(billdetails);
        }
        public string InsertBillDetail(BillDetail b)
        {
            BillDetail dt=null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    dt=ctx.BillDetails.Where(a => a.BillID == b.BillID && a.Status.CompareTo("Đã xóa") == 0).FirstOrDefault<BillDetail>();
                    if (dt != null)
                    {
                        dt.Status = "Đang tồn tại";
                        dt.UnitPrice = b.UnitPrice;
                        dt.Quantity = b.Quantity;
                        ctx.SaveChanges();
                    }
                    else
                    {
                        ctx.BillDetails.Add(b);
                        ctx.SaveChanges();
                    }
                    return "Thêm thành công";
                }
                catch
                {
                    return "Thêm thất bại";
                }
            }
        }
        public String DeleteToBill(int bill)
        {
            BillDetail b = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                b = ctx.BillDetails.Where(a => a.BillID == bill).FirstOrDefault<BillDetail>();
                if (b != null)
                {                    
                    ctx.SaveChanges();
                    return "Xóa thành công!";
                }
                else
                    return "Hóa đơn không tồn tại";
            }
        }
        public string UpdateStatus(BillDetail billdt)
        {
           List<BillDetail> billdetail = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                billdetail = ctx.BillDetails.Where(a => a.BillID == billdt.BillID&&a.ProductID.CompareTo(billdt.ProductID)==0).ToList<BillDetail>();
                if (billdetail != null)
                {
                    foreach (var item in billdetail)
                    {
                        item.Status = billdt.Status;
                        item.UnitPrice = billdt.UnitPrice;
                        item.Quantity = billdt.Quantity;
                        ctx.SaveChanges();
                    }
                    return "Cập nhật thành công!";
                }
                else
                    return "Cập nhật thất bại";
            }
        }
        public string UpdateStatus(Bill bill)
        {
            List<BillDetail> billdetail = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                billdetail = ctx.BillDetails.Where(a => a.BillID == bill.BillID).ToList<BillDetail>();
                if (billdetail != null)
                {
                    foreach (var item in billdetail)
                    {
                        item.Status = "Đã xóa";
                        ctx.SaveChanges();
                    }
                    return "Xóa thành công!";
                }
                else
                    return "Xóa thất bại";
            }
        }
    }
}
